﻿using System;
using System.Data;
using System.Windows.Forms;
using Elkomp;

namespace Controls {

	public partial class openBox : UserControl {

		public openBox() {
			InitializeComponent();
			if ( showRedIfEmpty == true ) {
				Value.C_ShowRedOnLeaveIfEmpty = true;
			} else {
				Value.C_ShowRedOnLeaveIfEmpty = false;
			}
		}

		public bool showRedIfEmpty { set; get; }

		public string formToOpen { set; get; }

		public string GridFromForm { set; get; }

		public string AssemblyStr { set; get; }

		public string CaptionText { set; get; }

		public string ColumnName1 { set; get; }

		public string ColumnName2 { set; get; }

		public string ColumnName3 { set; get; }

		public string ColumnName4 { set; get; }

		public string ColumnName5 { set; get; }

		public string ColumnName6 { set; get; }

		public string ColumnName7 { set; get; }

		public string ret1 = "";
		public string ret2 = "";
		public string ret3 = "";
		public string ret4 = "";
		public string ret5 = "";
		public string ret6 = "";
		public string ret7 = "";

		public bool IsTextEmpty( String txt ) {
			return string.IsNullOrEmpty( txt );
		}

		public delegate void doneLoading();
		public event doneLoading DataRead;
		public bool read;

		public string Text { set; get; }

		private void OpenForm( object sender, EventArgs e ) {
			read = false;

			System.Reflection.Assembly asm = System.Reflection.Assembly.Load( AssemblyStr );
			Type t = asm.CreateInstance( formToOpen, true ).GetType();
			FormaBrowse frm = ( FormaBrowse )Activator.CreateInstance( t, new object[] { true } );
			frm.ShowDialog();
			DataRow dtr;
			dtr = frm.dr;
			frm.Dispose();

			if ( frm.PrifatiKlik == true ) {
				Value.Text = dtr[ColumnName1].ToString();
				Text = Value.Text;

				try {
					if ( !IsTextEmpty( ColumnName1 ) ) { ret1 = dtr[ColumnName1].ToString(); }
					if ( !IsTextEmpty( ColumnName2 ) ) { ret2 = dtr[ColumnName2].ToString(); }
					if ( !IsTextEmpty( ColumnName3 ) ) { ret3 = dtr[ColumnName3].ToString(); }
					if ( !IsTextEmpty( ColumnName4 ) ) { ret4 = dtr[ColumnName4].ToString(); }
					if ( !IsTextEmpty( ColumnName5 ) ) { ret5 = dtr[ColumnName5].ToString(); }
					if ( !IsTextEmpty( ColumnName6 ) ) { ret6 = dtr[ColumnName6].ToString(); }
					if ( !IsTextEmpty( ColumnName7 ) ) { ret7 = dtr[ColumnName7].ToString(); }
				}
				catch ( Exception ex ) {
					MessageBox.Show( ex.ToString() );
				}
				read = true;
				this.DataRead.Invoke();
			}
		}

		private void txtNum1_KeyUp( object sender, KeyEventArgs e ) {
			if ( e.KeyCode == Keys.Enter ) {
				Open.PerformClick();
			}
		}
	}
}